﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Prsrp
{
    public class BaseCollectable : BaseAsset
    {
        #region States Definitions
    
        public enum CollectableStates
        {
            kIdle = 0,
            kFalling
        }
        public CollectableStates mpCurState = CollectableStates.kFalling;

        #endregion

        #region Constructeurs

        public BaseCollectable()
        {
            LoadSprites();
            mvSize = new Vector2(mtabStateSprites[0].Width, mtabStateSprites[0].Height);
        }

        #endregion

        #region States Methods

        public void HandleHeroCollision()
        {
            // todo: check for collision detection with hero.
            foreach (BaseHero pCurHero in Game.Engine.CurrentLevel.Characters)
            {
                Rectangle pCurHitBox = GetHitBox();
                if (pCurHitBox.Intersects(pCurHero.GetHitBox()))
                {
                    OnPicked(pCurHero);
                }
            }
        }

        #endregion

        public virtual void OnPicked(BaseHero _pHero)
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            switch (mpCurState)
            {
               case CollectableStates.kFalling:
               {
                   mvSpeed.Y += GameGlobals.Gravity;
                   // Fake Floor Collision Handling
                   float fFloorLevel = GameGlobals.GameHeight - (mvSize.Y / 2.0f * mfScale) - 1;
                   if (mvPos.Y > fFloorLevel)
                   {
                       mvSpeed.X *= 0.25f;
                       mvSpeed.Y = 0;
                       mpCurState = CollectableStates.kIdle;
                       mvPos.Y = fFloorLevel;
                   }
                   break;
               }
            }
            // Set Pos
            mvSpeed *= GameGlobals.AirFriction;
            mvPos = mvPos + mvSpeed;

            HandleHeroCollision();
        }

        public override void Draw()
        {
            mtabStateSprites[0].Draw(mvPos, mfScale, !mbFlipSprite);
        }
    }
}
